<?php include _include(ADMIN_PATH.'view/htm/header.inc.htm');?>
<!--{hook admin_content_post_start.htm}-->
<div class="container-fluid">
    <div class="row">
        <div class="col-lg-12">
            <!--{hook admin_content_post_breadcrumb_before.htm}-->
            <?php if(empty($hide_breadcrumb)) { ?>
            <ol class="breadcrumb d-none d-md-flex text-muted border-left-primary">
                <li class="breadcrumb-item"><a href="<?php echo url('content-list', '', TRUE);?>"><i class="icon-home text-primary" aria-hidden="true"></i></a></li>
                <?php if ($fid) { ?>
                <li class="breadcrumb-item"><?php echo $forum['name'];?></li>
                <?php } ?>
                <li class="breadcrumb-item"><?php echo $breadcrumb_flag;?></li>
                <!--{hook admin_content_post_breadcrumb.htm}-->
            </ol>
            <?php } ?>
            <!--{hook admin_content_post_breadcrumb_after.htm}-->
            <div class="card card-threadlist">
                <div class="card-body">
                    <!--{hook admin_content_post_form_start.htm}-->
                    <?php include _include(ADMIN_PATH.'view/htm/thread_post.inc.htm');?>
                    <!--{hook admin_content_post_form_end.htm}-->
                </div>
            </div>
        </div>
    </div>
</div>
<!--{hook admin_content_post_end.htm}-->

<?php include _include(ADMIN_PATH.'view/htm/footer.inc.htm');?>

<script>
    /* 模型 */
    var model = <?php echo $model;?>;
    var referer = '<?php echo $referer;?>';
    /* 主题 */
    var tid = '<?php echo GLOBALS('tid');?>';
    var forumlist = <?php echo xn_json_encode($forumlist);?>;

    /*初始化值，选中的值*/
    var index_flagids = <?php echo xn_json_encode($index_flagids);?>;
    var index_flag_list = <?php echo xn_json_encode($index_flag);?>;

    var forum_flagids = <?php echo xn_json_encode($forum_flagids);?>;
    var category_flagids = <?php echo xn_json_encode($category_flagids);?>;

    var action = '<?php echo $action;?>';
    var safe_token = '<?php echo $safe_token;?>';

    var jfid = $('select[name="fid"]');
    var fid = <?php echo intval($fid); ?> || jfid.checked();
    jfid.on('change', function() {
        var fid = jfid.checked();
        fid_on_change(fid);
    });

    fid_on_change(fid);

    /* 当选择栏目时，加载对应需要加载的代码 */
    function fid_on_change(fid) {
        var forum = forumlist[fid];

        <!--{hook admin_content_js_on_change_start.htm}-->

        /* 如果不是文章模型 则跳转重新加载添加内容界面 */
        if (forum.model != model) {
            if (action == 'create') {
                window.location = xn.url('content-' + action, {fid:fid}, true);
            } else {
                var tid = '<?php echo GLOBALS('tid');?>';
                window.location = xn.url('content-' + action, {tid:tid}, true);
            }
            return false;
        }

        <!--{hook admin_content_js_on_change_middle.htm}-->

        var s = '<div class="col-lg-12">';
        var fup = forumlist[forum.fup];
        if (!xn.empty(forum.fup) && !xn.empty(fup.flagstr)) {
            s += ' <p class="font-weight-bold mb-2"><?php echo lang('channel').lang('flag');?></p>';
            s += '<p class="tag-outline-list">';
            $.each(fup.flagstr_fmt, function(k, flag) {
                s += '<a href="javascript:void(0);" class="btn btn-outline-secondary btn-sm mr-3 mb-2 flag-option ' + (xn.in_array(flag.flagid, category_flagids) ? 'active' : '') + '" flagid="' + flag.flagid + '">' + flag.name + '</a>';
                s += '<input type="hidden" name="category[]" value="' + (xn.in_array(flag.flagid, category_flagids) ? flag.flagid : '') + '" />';
            });
            s += '</p>';
        }

        if (xn.isset(forum.flagstr_fmt)) {
            s += ' <p class="font-weight-bold mb-2"><?php echo lang('column').lang('flag');?></p>';
            s += '<p class="tag-outline-list">';
            $.each(forum.flagstr_fmt, function(k, flag) {
                s += '<a href="javascript:void(0);" class="btn btn-outline-secondary btn-sm mr-3 mb-2 flag-option ' + (xn.in_array(flag.flagid, forum_flagids) ? 'active' : '') + '" flagid="' + flag.flagid + '">' + flag.name + '</a>';
                s += '<input type="hidden" name="forum[]" value="' + (xn.in_array(flag.flagid, forum_flagids) ? flag.flagid : '') + '" />';
            });
            s += '</p>';
        }
        s += '</div>';
        $('.flag-list').html(s);

        <!--{hook admin_content_js_on_change_end.htm}-->
    }

    <?php if (!empty($index_flag)) { ?>
        index_flag();
    <?php } ?>

    function index_flag() {
        var s = '<div class="col-lg-12">';
        s += ' <p class="font-weight-bold pt-0 mb-2"><?php echo lang('index_page').lang('flag');?></p>';
        s += '<p class="tag-outline-list">';
        $.each(index_flag_list, function(k, flag) {
            s += '<a href="javascript:void(0);" class="btn btn-outline-secondary btn-sm mr-3 mb-2 flag-option ' + (xn.in_array(flag.flagid, index_flagids) ? 'active' : '') + '" flagid="' + flag.flagid + '">' + flag.name + '</a>';
            s += '<input type="hidden" name="index[]" value="' + (xn.in_array(flag.flagid, index_flagids) ? flag.flagid : '') + '" />';
        });
        s += '</p>';
        s += '</div>';
        $('.index-flag-list').html(s);
    }

    $('.flag-list,.index-flag-list').on('click', 'a.flag-option', function() {
        var jthis = $(this);
        var flagid = jthis.attr('flagid');
        jthis.toggleClass('active');
        /*隐藏域*/
        var v = jthis.hasClass('active') ? flagid : '';
        jthis.next().attr('value', v);
    });

    var jform = $('#form');
    jform.on('submit', function() {
        jform.reset();
        var jthis = $(this);
        var jsubmit = jthis.find('#submit');
        jsubmit.button('loading');
        var action = '<?php echo $action;?>';
        var postdata = jform.serialize();
        if(action == 'create') var fid = jfid.val();
        $.xpost(jform.attr('action'), postdata, function(code, message) {
            if(code == 0) {
                $.alert(message);
                var referer = referer || xn.url('content-list', '', true);
                jsubmit.button(message).delay(1000).location(referer);
            } else if(xn.is_number(code)) {
                $.alert(message);
                jsubmit.button('reset');
            } else {
                $.alert(message);
                jsubmit.button('reset');
            }
        });
        return false;
    });

    var addicon = $('.addicon');
    addicon.on('change', 'input[type="file"]', function(e) {
        var jfile = $(this);
        var jassoc = jfile.data('assoc') ? $('#'+jfile.data('assoc')) : null;
        var n = $('img#'+jfile.data('assoc')).attr('n');
        var files = xn.get_files_from_event(e);
        if (!files) return;

        var fid = jfid.checked();
        var forum = forumlist[fid];
        var picture_size = forum.thumbnail;

        $.each_sync(files, function (i, callback) {
            var file = files[i];
            xn.upload_file(file, xn.url('attach-create-1', '', 2), {
                'fid': fid,
                'is_image': 1,
                'convert': 1, /*转码*/
                'filetype': 'jpeg',   /*压缩类型*/
                'qulity': '0.7',  /*压缩比例*/
                'width': picture_size.width || "<?php echo $pic_width; ?>",
                'height': picture_size.height || "<?php echo $pic_height; ?>",
                'action': "<?php echo array_value($conf, 'upload_resize', 'clip'); ?>",
                'n': n,
                'mode': 1, /*缩略图*/
                'safe_token': safe_token
            }, function (code, message) {
                if (code != 0) return $.alert(message);
                jassoc.attr('src', message.url);
                /*jassoc.css('max-width',150);
                 jassoc.css('height','auto');*/
                callback();
            }, function (percent) {
                /*percent = xn.intval(percent);
                 jprogressbar.css('width', percent+'%');
                 jprogressbar.text(percent+'%');
                 jprogress.show();
                 console.log('progress:'+ percent);*/
            }, function (base64_data) {
                /*base64_data 用来显示缩略图*/
            });
        });
    });

    var jattachparent = $('.attachlist_parent');
    $('#addattach').on('change', function(e) {
        var files = xn.get_files_from_event(e);
        if (!files) return;

        /*并发下会 服务端 session 写入会有问题，由客户端控制改为串行*/
        if (!jattachparent.find('.attachlist').length) {
            jattachparent.append('<fieldset class="fieldset m-0 p-0"><legend><?php echo lang('uploaded_attach');?></legend><ul class="list-unstyled attachlist nowrap"><ul></fieldset>');
        }

        var jprogress = jattachparent.find('.progress');
        if(!jprogress.length) {
            jprogress = $('<div class="progress"><div class="progress-bar" role="progressbar" style="width: 25%;" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100">25%</div></div>').appendTo(jattachparent);
        }
        jprogressbar = jprogress.find('.progress-bar');

        var fid = jfid.checked();
        $.each_sync(files, function(i, callback) {
            var file = files[i];
            xn.upload_file(file, xn.url('attach-create-1', '', 2), {'fid': fid,'is_image': 0,'safe_token': safe_token}, function (code, message) {
                if (code != 0) return $.alert(message);
                /*把文件 append 到附件列表*/
                var url = message.url;
                var filetype = message.filetype;
                var aid = message.aid;
                var s = '<a href="' + url + '" target="_blank" class="d-block ellipsis"><i class="icon filetype ' + filetype + '"></i> ' + message.orgfilename + '</a>';
                <!--{hook admin_content_post_attach_before.htm}-->
                s += '<span class="btn px-1 py-0 delete"><i class="icon-remove"></i></span>';
                <!--{hook admin_content_post_attach_after.htm}-->
                var attachlist = '<li aid="' + aid + '" class="d-flex justify-content-between p-1">' + s +'</li>';
                $('.attachlist').append(attachlist);
                callback();
                jprogress.hide();
            }, function(percent) {
                percent = xn.intval(percent);
                jprogressbar.css('width', percent+'%');
                jprogressbar.text(percent+'%');
                jprogress.show();
                console.log('progress:'+ percent);
            });
        });
    });

    /*删除附件*/
    jattachparent.on('click', 'span.delete', function() {
        var jlink = $(this);
        var jli = jlink.parents('li');
        var aid = jli.attr('aid');
        if(!window.confirm(lang.confirm_delete)) return false;
        $.xpost(xn.url('attach-delete-'+aid, '', 2), function(code, message) {
            if(code != 0) return $.alert(message);
            jlink.parent().remove();
        });
        return false;
    });

    $('#brief,#keyword,#description').on('focus', function () {
        var jthis = $(this);
        if ($(this).t) {
            clearTimeout($(this).t);
            $(this).t = null;
        }
        jthis.css('height', '5rem');
        jthis.blur(function () {
            jthis.t = setTimeout(function () {
                jthis.css('height', '2.5rem');
            }, 1000);
        });
    });

    var input_link = $('input[name="link"]');
    input_link.on('click', function () {
        var message = $('.form-message,.keyword');
        var attach = $('#nav-attach-tab');
        var description = $('div.description');
        if ($(this).checked() == true) {
            var copy = description.clone();
            copy.find('textarea').attr('placeholder', "<?php echo lang('external_link');?>");
            $('.form-message').after(copy);
            message.hide(300);
            attach.hide();
        } else {
            var copy = description.clone();
            copy.find('textarea').attr('placeholder', "SEO<?php echo lang('description') . lang('exceed_limit_will_be_intercepted', array('n' => 120)) .'('.lang('description_empty_use_brief').')';?>");
            $('.keyword').after(copy);
            message.show(300);
            attach.show();
        }
        description.remove();
    });

    if (action == 'update') {
        if (input_link.checked() == true) {
            var description = $('div.description');
            var copy = description.clone();
            description.remove();
            copy.find('textarea').attr('placeholder', "<?php echo lang('external_link');?>");
            $('.form-message').after(copy);
            $('.form-message,.keyword').hide();
            $('#nav-attach-tab').hide();
        }
    }

    jform.find('[name="fid"]').checked(fid);

    if ($(document.body).width() < 992) {
        $('#nav-show').css({"position": "fixed", "top": "0", "bottom": "0", "right": "0", "margin-top": "3.625rem", "z-index": "1032"});
    }

    document.getElementById('thumbnail-delete').addEventListener('click', function (e) {
        let url = this.getAttribute('data-href');
        $.xget(url, function (code, message) {
            if (0 == code) {
                document.querySelector('.img-thumbnail').setAttribute('src', message.thumbnail);
            } else {
                $.alert(message.message);
            }
        });
        e.stopPropagation();
    });

    $('#content').addClass('show');
    $('li.<?php echo $route;?>').addClass('active');
</script>

<!--{hook admin_content_post_js.htm}-->